//
//  HelpHeaderView.swift
//  AcrossAnHui
//
//  Created by 张继强 on 16/11/17.
//  Copyright © 2016年 安徽畅通行. All rights reserved.
//

import UIKit

typealias ClickBlock  = (HelpCenterModel)->Void

class HelpHeaderView: UITableViewHeaderFooterView {
    var rightImage:UIImageView?
    var infoLabel:UILabel?
    var showButton:UIButton?
    var block:ClickBlock?
    var shawDownView:UIView = UIView.init()
    var model:HelpCenterModel?{
        didSet{
            //布局
//            self.model?.content = "我要找谁去解决啊"
//            self.model?.selected = false
            if (self.model?.selected)! {
                //选中状态
                self.rightImage?.image = UIImage.init(named: "ic_next_click")
                self.infoLabel?.textColor = .red
            }else{
                //未选中状态
                self.rightImage?.image = UIImage.init(named: "ic_next")
                self.infoLabel?.textColor = .black
            }

//            self.leftImage?.snp.makeConstraints({ (make) in
//                make.left.equalTo(12.5)
//                make.size.equalTo(CGSize.init(width: 15, height: 15))
//                make.top.equalTo(20)
//            })
//            let width = (self.infoLabel?.width(forText: model?.title, withFontSize: 15, withTextHeight: 15))! + 0.1
//            self.infoLabel?.snp.makeConstraints({ (make) in
//                make.left.equalTo((self.leftImage?.snp.right)!).offset(7)
//                make.width.equalTo(width)
//                make.top.equalTo(20)
//                make.height.equalTo(15)
//            })
//            self.rightImage?.snp.makeConstraints({ (make) in
//                make.left.equalTo((self.infoLabel?.snp.right)!).offset(7)
//                make.size.equalTo(CGSize.init(width: 15, height: 15))
//                make.top.equalTo(20)
//            })
//            showButton?.snp.makeConstraints({ (make) in
//                make.top.equalTo(0)
//                make.left.equalTo(0)
//                make.right.equalTo(0)
//                make.bottom.equalTo(0)
//            })
            showButton?.backgroundColor = .clear
            self.infoLabel?.text = model?.title
            
        }
    }
    override init(reuseIdentifier: String?){
        super.init(reuseIdentifier: reuseIdentifier)
        self.rightImage = UIImageView.init()
        self.infoLabel = UILabel.init()
        self.showButton = UIButton.init()
        self.contentView.addSubview(self.rightImage!)
        self.contentView.addSubview(self.infoLabel!)
        self.contentView.addSubview(self.showButton!)
        self.contentView.addSubview(self.shawDownView)
        showButton?.addTarget(self, action: #selector(selectAction), for: .touchUpInside)
        self.rightImage?.image = UIImage.init(named: "helpxialw")
        self.contentView.backgroundColor = .white
        self.infoLabel?.font = UIFont.systemFont(ofSize: 15)
        self.shawDownView.backgroundColor = .lightGray
        self.shawDownView.snp.makeConstraints { (make) in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.bottom.equalTo(0)
            make.height.equalTo(0.5)
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let width = (self.infoLabel?.width(forText: self.infoLabel?.text, withFontSize: 15, withTextHeight: 15))! + 0.1+1
        self.infoLabel?.snp.makeConstraints({ (make) in
            make.left.equalTo(16)
            make.width.equalTo(width)
            make.top.equalTo(12)
            make.height.equalTo(15)
        })
        self.rightImage?.snp.makeConstraints({ (make) in
            make.left.equalTo((self.infoLabel?.snp.right)!).offset(7)
            make.size.equalTo(CGSize.init(width: 12, height: 7))
            make.top.equalTo(16.5)
        })
        showButton?.snp.makeConstraints({ (make) in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.bottom.equalTo(0)
        })

    }
    
    func selectAction(){
//       print("按钮被点击")
        self.model?.selected = !(self.model?.selected)!
        if (self.model?.selected)! {
            //选中状态
            self.rightImage?.image = UIImage.init(named: "helpxiala")
            self.infoLabel?.textColor = .red
        }else{
            //未选中状态
            self.rightImage?.image = UIImage.init(named: "helpxialw")
            self.infoLabel?.textColor = .black
        }
        if block != nil {
            block!(self.model!)
        }
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}
